<template>
  <div class="cart py-container">
    <!--主内容-->
    <div class="checkout py-container  pay">
      <div class="checkout-tit">
        <h4 class="fl tit-txt"><span class="success-icon"></span><span class="success-info">订单提交成功，请您及时付款！订单号：{{payObj.out_trade_no}}</span>
        </h4>
        <span class="fr"><em class="sui-lead">应付金额：</em><em class="orange money">￥{{payObj.total_fee}}</em></span>
        <div class="clearfix"></div>
      </div>
      <div class="checkout-steps">
        <div>
          请选择支付方式：
          <el-radio v-model="payType" label="1" :class="[{'is-checked':payType==1}]" border @change="changPayType()">
            <label class="pay-lab pay-wx"></label>
          </el-radio>
          <el-radio v-model="payType" label="2" :class="[{'is-checked':payType==2}]" border
                    @change="changPayType()">
            <label class="pay-lab pay-ali"></label>
          </el-radio>
        </div>
        <div class="fl weixin">{{payTypeText}}支付</div>
        <div class="fl sao">
          <p class="red">请使用{{payTypeText}}扫一扫。</p>
          <div class="fl code">
            <!-- <img id="qrious" src="~/assets/img/erweima.png" alt=""> -->
            <!-- <qriously value="weixin://wxpay/bizpayurl?pr=R7tnDpZ" :size="338"/> -->
            <qriously :class="[payType==1?'wxQr':'aliQr']" v-if="payObj.code_url" :value="payObj.code_url" :size="338"/>
            <div class="saosao">
              <p>请使用{{payTypeText}}扫一扫</p>
              <p>扫描二维码支付</p>
            </div>

          </div>

        </div>
        <div class="clearfix"></div>
        <!-- <p><a href="pay.html" target="_blank">> 其他支付方式</a></p> -->

      </div>
    </div>
  </div>
</template>
<script>
  import orderApi from '@/api/order'

  export default {
    data() {
      return {
        orderId: this.$route.params.pid,
        payObj: {},
        timer: null,  // 定时器名称
        initQCode: '',
        timer1: '',
        payType: '2',
        payTypeText:  this.payType === '1' ? '微信' : '支付宝沙箱版'
      }
    },
    mounted() {
      // this.createQrByAli();
      this.createQr();
      //在页面渲染之后执行
      //每隔三秒，去查询一次支付状态
      this.timer1 = setInterval(() => {
        this.queryPayStatus(this.payObj.out_trade_no)
      }, 3000);
    },
    beforeDestroy() {
      if(this.timer1) { //如果定时器还在运行 或者直接关闭，不用判断
        clearInterval(this.timer1); //关闭
      }
    },
    methods: {
      changPayType() {
        this.payTypeText = this.payType === '1' ? '微信' : '支付宝'
        this.createQr();
      },
      createQr() {
        /*code_url: "weixin://wxpay/bizpayurl?pr=h6WQ90yzz"
                  course_id: "18"queryPayStatusByAlipay
                  out_trade_no: "20210624161132342"
                  result_code: "SUCCESS"
                  total_fee: 0.01*/
        if (this.payType === '1') {
          //申请wx二维码
          orderApi.createNative(this.orderId).then(response => {
            this.payObj = response
          })
        } else {
          //申请支付宝二维码
          orderApi.createNativeByAliPay(this.orderId).then(response => {
            this.payObj = response
          })
        }


      },
      //查询支付状态的方法
      queryPayStatus(out_trade_no) {

        let callbackFn=response => {
          if (response && response.success) {
            //如果支付成功，清除定时器
            clearInterval(this.timer1)
            this.$message({
              type: 'success',
              message: '支付成功!'
            })
            //跳转到课程详情页面观看视频
            this.$router.push({path: `/${this.$enum.getObjByValue('COURSE_TYPE', this.payObj.target_type).path}/` + this.payObj.course_id})
          }
        };

        if (this.payType === '1') {
          orderApi.queryPayStatus(out_trade_no).then(callbackFn)
        } else {
          orderApi.queryPayStatusByAliPay(out_trade_no).then(callbackFn)
        }
      }
    }
  }
</script>

<style scoped>
  .wxQr {
    border: 5px #3db922 solid;
  }

  .aliQr {
    border: 5px #00a8e9 solid;
  }

  /*支付按钮*/
  .pay-lab {
    display: inline-block;
    width: 122px;
    height: 37px;
    background-size: 122px 37px;

  }

  .pay-ali {
    background-image: url();
  }

  .pay-wx {
    background-image: url();
  }

  /deep/ .el-radio__input.is-checked .el-radio__inner {
    display: none;
  }

  /deep/ .el-radio__inner {
    display: none;
  }

  /deep/ .el-radio.is-bordered {
    padding: 0;
  }
</style>
